import java.util.Arrays;
public class MyArrayList implements IList {
    public int[] elem;
    public int usedSize;

    public MyArrayList(){
        this.elem=new int[Constant.DEFAULT_CAPACITY];
    }
    @Override
    public void add(int data){
        if(isFull()){
            grow();
        }
        this.elem[this.usedSize]=data;
        this.usedSize++;
    }

    public boolean isFull(){
        return this.usedSize==this.elem.length;
    }
    public void grow(){
        this.elem=Arrays.copyOf(this.elem,2*this.elem.length);
    }
    // 在 pos 位置新增元素
   @Override
    public void add(int pos,int data){
        if(isFull()){
            grow();
        }
       String msg = Constant.ADD_POS_ILLEGALITY+pos;
       checkPosAdd(pos,msg);
       //2.判断是不是放在usedSize的位置
       if(pos == usedSize) {
           elem[pos] = data;
           usedSize++;
           return;}
       for (int i = usedSize-1; i >= pos; i--) {
           elem[i+1] = elem[i];
       }
       elem[pos] = data;
       usedSize++;
   }
   private void checkPosAdd(int pos,String msg) {
        if(pos < 0 || pos > usedSize) {
            throw new PosIllegalityException(msg);
        }
    }

    @Override
    public boolean contains(int toFind){
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    @Override
    public int indexOf(int toFind){
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }
    // 获取 pos 位置的元素
    @Override
    public int get(int pos){
        if(isEmpty()){
            throw new EmptyListException();
        }
        String msg = Constant.GET_POS_ILLEGALITY+pos;
        checkPos(pos,msg);
        return elem[pos];
    }
    public boolean isEmpty() {
        return usedSize == 0;
    }
    private void checkPos(int pos,String msg){
        if(pos<0||pos>=usedSize){
            throw new PosIllegalityException(msg);
        }
    }
    // 给 pos 位置的元素设为 value
    @Override
    public void set(int pos, int value){
        if(isEmpty()) {
            throw new EmptyListException(Constant.EMPTY_LIST);
        }

        String msg = Constant.SET_POS_ILLEGALITY+pos;
        checkPos(pos,msg);

        elem[pos] = value;
    }
    //删除第一次出现的关键字key
    @Override
    public void remove(int toRemove){
      if(isEmpty()){
          throw new EmptyListException(Constant.EMPTY_LIST);
      }
      int index=indexOf(toRemove);
      if(index==-1){
          System.out.println("没有你要找的数据");
          return;
      }
        for (int i = index; i < usedSize-1; i++) {
            elem[i]=elem[i+1];
        }
        usedSize--;
    }
    // 获取顺序表长度
    @Override
    public int size(){
        return this.usedSize;
    }
    // 清空顺序表
    @Override
    public void clear(){
        for (int i = 0; i < usedSize; i++) {
            elem[i]=0;
        }
        usedSize=0;
    }
    // 打印顺序表，注意：该方法并不是顺序表中的方法，为了方便看测试结果给出的
    @Override
    public void display(){
        for (int i = 0; i <this.usedSize; i++) {
            System.out.println(this.elem[i]+" ");
        }
        System.out.println();
    }
}

